package jgl.vajra.operator.part.mine.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.CommissionDetailsBean;
import jgl.vajra.operator.bean.CommissionDetailsHeadBean;
import jgl.vajra.operator.databinding.ActivityCommissionDetailsBinding;
import jgl.vajra.operator.listener.MyToolbarViewListener;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.part.mine.contract.CommissionDetailsContract;
import jgl.vajra.operator.part.mine.pop.CommissionDetailsPop;
import jgl.vajra.operator.part.mine.viewModel.CommissionDetailsViewModel;
import jgl.vajra.operator.widget.base.BaseListActivity;
import jgl.vajra.operator.widget.bean.BaseListData;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(CommissionDetailsViewModel.class)
public class CommissionDetailsActivity extends BaseMVVMActivity<CommissionDetailsViewModel, ActivityCommissionDetailsBinding> implements CommissionDetailsContract.View, BaseBindingItemPresenter<CommissionDetailsBean> {

    private SingleTypeBindingAdapter adapter;
    private String orderBy = "";
    private List<CommissionDetailsHeadBean> list;
    private SingleTypeBindingAdapter adapterHead;
    private CommissionDetailsPop commissionDetailsPop;
    private String type = "";

    @Override
    public void onItemClick(int position, CommissionDetailsBean itemData) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_commission_details;
    }

    @Override
    public void initView() {
        super.initView();

        list = new ArrayList<>();
        list.add(new CommissionDetailsHeadBean("全部佣金", "", true));
        list.add(new CommissionDetailsHeadBean("客户端分佣", "1", false));
        list.add(new CommissionDetailsHeadBean("机主端分佣", "2", false));
        list.add(new CommissionDetailsHeadBean("司机端分佣", "3", false));
        list.add(new CommissionDetailsHeadBean("已提现佣金", "4", false));
        //  list.add(new CommissionDetailsHeadBean("佣金余额", false));

        mBinding.toolbar.setMyToolbarViewListener(new MyToolbarViewListener() {
            @Override
            public void onRightTextClick() {
                super.onRightTextClick();
                /*cs="hhhhh";
                mBinding.recyclerView.firstLoad();*/

                /**按照字母排序**/
              /*  Collections.sort(list, (o1, o2) -> {
                    String s1 = o1.title;
                    String s2 = o2.title;
                    return Collator.getInstance(Locale.CHINESE).compare(s1, s2);
                });
                adapterHead.refresh();*/

                commissionDetailsPop = new CommissionDetailsPop(CommissionDetailsActivity.this);
                commissionDetailsPop.setData(orderBy);
                commissionDetailsPop.setCommissionDetailsListener((itemData, itemOrderBy) -> {
                    orderBy = itemOrderBy;
                    mBinding.recyclerView.firstLoad();
                });
                commissionDetailsPop.showPopupWindow(mBinding.toolbar);
            }
        });

        // 不可嵌套滑动
        mBinding.recyclerViewHead.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewHead.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewHead.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        adapterHead = new SingleTypeBindingAdapter(this, list, R.layout.item_commission_details_head);
        mBinding.recyclerViewHead.setAdapter(adapterHead);
        adapterHead.setItemPresenter(this);


        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setIsShowPageManager(true);
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_commission_details);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("type", type);
                if (!TextUtils.isEmpty(orderBy))
                    map.put("orderBy", orderBy);
                return mViewModel.getCommissionDetailsListData(map);
            }
        });

        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<CommissionDetailsBean>>() {
            @Override
            public void onSucceed(BaseListData<CommissionDetailsBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                for (int i = 0; i < data.data.size(); i++) {
                    if ("4".equals(type)){
                        data.data.get(i).typeText="-";
                    }
                }

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
                //mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
    }

    @Override
    public void onTitleClick(int position, CommissionDetailsHeadBean itemData) {
        if (itemData.isClick) return;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).isClick = false;
        }
        list.get(position).isClick = true;
        adapterHead.refresh();

        type = itemData.type;
        mBinding.recyclerView.firstLoad();
    }
}
